<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" >
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="Pls inpt ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="LLM" prop="llm">
        <el-select v-model="queryParams.llm" placeholder="Select LLM" clearable>
          <el-option
            v-for="dict in dict.type.llm_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="PromptType" prop="promptType">
        <el-select v-model="queryParams.promptType" placeholder="Select PromptType" clearable>
          <el-option
            v-for="dict in dict.type.prompt_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Case Type" prop="type">
        <el-select v-model="queryParams.type" placeholder="Select Case Type" clearable>
          <el-option
            v-for="dict in dict.type.ade_case_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ade:case:add']"
        >New</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ade:case:edit']"
        >Edit</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ade:case:remove']"
        >Delete</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ade:case:export']"
        >Export</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="42" align="center"/>
      <!--
      <el-table-column label="ID" align="center" width="60" prop="id" />
      -->
      <el-table-column label="Content" align="left" prop="content" show-overflow-tooltip=""/>
      <el-table-column label="Ades" align="left" prop="ades" show-overflow-tooltip=""/>
      <el-table-column label="LLM" align="center" prop="llm" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.llm_model" :value="scope.row.llm"/>
        </template>
      </el-table-column>
      <el-table-column label="PromptType" align="center" prop="promptType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prompt_type" :value="scope.row.promptType"/>
        </template>
      </el-table-column>
      <el-table-column label="Case Type" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ade_case_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="Source" align="left" prop="source" width="100" show-overflow-tooltip="">
      </el-table-column>
      <el-table-column label="Description" align="left" prop="description" show-overflow-tooltip=""/>
      <el-table-column label="CreateBy" align="center" width="80" prop="createBy" show-overflow-tooltip/>
       <el-table-column label="Create Time" align="center" width="140" prop="createTime" />
      <el-table-column label="Actions" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ade:case:edit']"
          >Edit</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ade:case:remove']"
          >Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Ade Cases对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" >
        <el-form-item label="Content" prop="content">
          <el-input v-model="form.content" type="textarea" rows="7" placeholder="Pls input content" />
        </el-form-item>
        <el-form-item label="Ades" prop="ades">
          <el-input v-model="form.ades" type="textarea" rows="7" placeholder="Pls input ades" />
        </el-form-item>
        <el-form-item label="LLM" prop="llm">
          <el-select v-model="form.llm" placeholder="Select LLM">
            <el-option
              v-for="dict in dict.type.llm_model"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="PromptType" prop="promptType">
          <el-select v-model="form.promptType" placeholder="Select PromptType">
            <el-option
              v-for="dict in dict.type.prompt_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Case Type" prop="type">
          <el-select v-model="form.type" placeholder="Selct Case Type">
            <el-option
              v-for="dict in dict.type.ade_case_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Description" prop="description">
          <el-input v-model="form.description" type="textarea" row="5" placeholder="Pls input description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">Confirm</el-button>
        <el-button @click="cancel">Cancel</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCase, getCase, delCase, addCase, updateCase } from "@/api/ade/case";

export default {
  name: "Case",
  dicts: ['ade_case_type', 'prompt_type', 'llm_model'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Ade Cases表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        id: null,
        content: null,
        ades: null,
        llm: null,
        promptType: null,
        type: null,
        description: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "Content不能为空", trigger: "blur" }
        ],
        ades: [
          { required: true, message: "Ades不能为空", trigger: "blur" }
        ],
        llm: [
          { required: true, message: "LLM不能为空", trigger: "change" }
        ],
        promptType: [
          { required: true, message: "PromptType不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "Case Type不能为空", trigger: "change" }
        ],
        description: [
          { required: true, message: "Description不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询Ade Cases列表 */
    getList() {
      this.loading = true;
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        ades: null,
        llm: null,
        promptType: null,
        type: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Ade Cases";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Ade Cases";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除Ade Cases编号为"' + ids + '"的数据项？').then(function() {
        return delCase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ade/case/export', {
        ...this.queryParams
      }, `case_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
